cmake_minimum_required(VERSION 3.16)

project(QtDemo VERSION 0.1.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(_targetName ${PROJECT_NAME})
message(STATUS "Target: ${_targetName}")

message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
#获取编译时间
string(TIMESTAMP COMPILE_TIME %Y%m%d%H%M%S)
message(STATUS "Build Time: ${COMPILE_TIME}")

#设置可执行文件的输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/target/Debug/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/target/Release/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_SOURCE_DIR}/target/MinSizeRel/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_SOURCE_DIR}/target/RelWithDebInfo/bin)
#设置库文件的输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/target/Debug/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/target/Release/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_SOURCE_DIR}/target/MinSizeRel/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_SOURCE_DIR}/target/RelWithDebInfo/lib)

option(ENABLE_AUTO_RUN_CPACK "Enable auto run cpack" OFF)
option(ENABLE_AUTO_RUN_DEPLOYQT "Enable auto deploy qt" ON)


# set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} D:/Qt/6.6.3/msvc2019_64/)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)


set(PROJECT_SOURCES
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    ${TS_FILES}
)

if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${_targetName}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
    # Define target properties for Android with Qt 6 as:
    #    set_property(TARGET ${_targetName} APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
    #                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
    # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation

    qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else ()
    if (ANDROID)
        add_library(${_targetName} SHARED
            ${PROJECT_SOURCES}
        )
        # Define properties for Android with Qt 5 after find_package() calls as:
        #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else ()
        add_executable(${_targetName}
            ${PROJECT_SOURCES}
        )
    endif ()

    qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif ()

target_link_libraries(${_targetName} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if (${QT_VERSION} VERSION_LESS 6.1.0)
    set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.${_targetName})
endif ()
set_target_properties(${_targetName} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
# install(TARGETS ${_targetName}
#     BUNDLE DESTINATION .
#     LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
#     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
# )

# 二进制文件
install(
    DIRECTORY ${CMAKE_SOURCE_DIR}/target/${CMAKE_BUILD_TYPE}/bin
    DESTINATION .
)

# 库文件
install(
    DIRECTORY ${CMAKE_SOURCE_DIR}/target/${CMAKE_BUILD_TYPE}/lib
    DESTINATION .
)
# 证书文件
install(
    FILES  ${CMAKE_CURRENT_SOURCE_DIR}/License
    DESTINATION .
)

if (QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${_targetName})
endif ()


# 包括 CPack 打包配置
include(InstallRequiredSystemLibraries) # 依赖的系统资源库
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License") # 证书文件
    set(CPACK_PACKAGE_DIRECTORY ${CMAKE_SOURCE_DIR}/target/package) #打包目录 
    # set(CPACK_GENERATOR "TGZ;ZIP;7Z;") # 打包格式，支持多种，以分号隔开
    set(CPACK_GENERATOR "ZIP") # 打包格式，支持多种，以分号隔开
    set(CPACK_PACKAGE_NAME "${_targetName}") # 包名称
    set(CPACK_PACKAGE_FILE_NAME ${_targetName}_${CMAKE_BUILD_TYPE}_${PROJECT_VERSION}_${COMPILE_TIME}) # 打包后的包名称
    include(CPack)
endif ()

if(ENABLE_AUTO_RUN_DEPLOYQT)
message(STATUS "Enable auto deploy qt")
if (WIN32)
add_custom_command(TARGET ${_targetName} POST_BUILD
    COMMAND Qt${QT_VERSION_MAJOR}::windeployqt ${CMAKE_SOURCE_DIR}/target/${CMAKE_BUILD_TYPE}/bin COMMENT "auto deploy" 
)
endif (WIN32)
endif(ENABLE_AUTO_RUN_DEPLOYQT)

if(ENABLE_AUTO_RUN_CPACK)
if (WIN32)
add_custom_command(TARGET ${_targetName} POST_BUILD 
    COMMAND cpack -C ${CMAKE_BUILD_TYPE}
)
endif (WIN32)
endif(ENABLE_AUTO_RUN_CPACK)
